package com.example.javademo;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.json.JSONObject;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_http);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


    }

    public static  void makeGetRequest(View view){
        NetworkUtils.makeGetRequest("https://m1.apifoxmock.com/m1/6876865-6591923-default/android/demolist", new NetworkUtils.NetworkCallback() {
            @Override
            public void onSuccess(String response) {
                Log.d("TAG", "onSuccess: 请求成功");
                Log.e("TAG", "原始响应: " + response);
                
                // 解析JSON响应
                try {
                    // 假设响应是JSON数组
                    if (response.startsWith("[")) {
                        org.json.JSONArray jsonArray = new org.json.JSONArray(response);
                        Log.d("TAG", "解析结果: 包含 " + jsonArray.length() + " 个元素");
                        // 打印第一个元素
                        if (jsonArray.length() > 0) {
                            org.json.JSONObject firstItem = jsonArray.getJSONObject(0);
                            Log.d("TAG", "第一个元素: " + firstItem.toString());
                        }
                    } 
                    // 假设响应是JSON对象
                    else if (response.startsWith("{")) {
                        org.json.JSONObject jsonObject = new org.json.JSONObject(response);
                        Log.d("TAG", "解析结果: " + jsonObject.toString());
                        // 示例：获取特定字段
                        if (jsonObject.has("title")) {
                            String title = jsonObject.getString("title");
                            Log.d("TAG", "标题: " + title);
                        }
                    } else {
                        Log.d("TAG", "响应不是有效的JSON格式");
                    }
                } catch (JSONException e) {
                    Log.e("TAG", "JSON解析失败: " + e.getMessage(), e);
                }
            }

            @Override
            public void onError(Exception e) {
                Log.e("TAG", "请求失败: " + e.getMessage(), e);
            }
        });
    }

    public static void makePostRequest(View  view){
        java.util.Map<String, String> formData = new java.util.HashMap<>();
        formData.put("userId", "1");
        formData.put("title", "test title");
        formData.put("body", "test body");

        NetworkUtils.makePostRequest("https://m1.apifoxmock.com/m1/6876865-6591923-default/android/setUser", formData, new NetworkUtils.NetworkCallback() {
            @Override
            public void onSuccess(String response) {
                Log.d("TAG", "POST表单请求成功: " + response);
                
                // 解析JSON响应
                try {
                    org.json.JSONObject jsonObject = new org.json.JSONObject(response);
                    Log.d("TAG", "解析结果: " + jsonObject.toString());
                    
                    // 示例：获取特定字段
                    if (jsonObject.has("id")) {
                        int id = jsonObject.getInt("id");
                        Log.d("TAG", "ID: " + id);
                    }
                    if (jsonObject.has("message")) {
                        String message = jsonObject.getString("message");
                        Log.d("TAG", "消息: " + message);
                    }
                } catch (JSONException e) {
                    Log.e("TAG", "JSON解析失败: " + e.getMessage(), e);
                }
            }

            @Override
            public void onError(Exception e) {
                Log.e("TAG", "POST表单请求失败: " + e.getMessage());
            }
        });
    }
}